<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\model\ProjectFace;
use app\model\ProjectHappy;
use app\model\ProjectHappyOut;
use app\model\ProjectHealthFood;
use app\model\ProjectHealthFoodNutrition;
use app\model\ProjectLife;
use app\model\ProjectLifeOut;
use app\model\ProjectMedical;
use app\model\ProjectRoom;
use DateTime;
use think\db\Query;
use think\facade\Log;
use think\Request;

class ProjectController extends BaseController
{
	protected $middleware = ['Login' => [
		'except' => [''],
	]];
	
	/**
	 * 面容健康
	 * @param Request $request
	 * @return void
	 */
	function face(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectFace::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$arr[] = ['date' => $formattedDate, 'list' => ProjectFace::whereDay('createtime', $row->time)->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select()];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 面容健康-详情
	 * @param Request $request
	 * @return void
	 */
	function faceDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectFace::findOrFail($id); //最新的一条
		$this->writeJson($row);
	}
	
	/**
	 * 居家检测
	 * @param Request $request
	 * @return void
	 */
	function room(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectRoom::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$arr[] = ['date' => $formattedDate, 'list' => ProjectRoom::whereDay('createtime', $row->time)->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select()];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 居家检测-详情
	 * @param Request $request
	 * @return void
	 */
	function roomDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectRoom::findOrFail($id); //最新的一条
		$this->writeJson($row);
	}
	
	/**
	 * 医疗报告
	 * @param Request $request
	 * @return void
	 */
	function medical(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectMedical::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$arr[] = ['date' => $formattedDate, 'list' => ProjectMedical::whereDay('createtime', $row->time)->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select()];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 医疗报告-详情
	 * @param Request $request
	 * @return void
	 */
	function medicalDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectMedical::findOrFail($id); //最新的一条
		$this->writeJson($row);
	}
	
	/**
	 * 营养健康
	 * @param Request $request
	 * @return void
	 */
	function health(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectHealthFood::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$foods = ProjectHealthFood::field('*,MAX(id) AS id')->whereDay('createtime', $row->time)->group('health_id')->where(['user_id' => $this->user_id])->select()->toArray();
			$foods_ids = array_column($foods, 'id');
			$arr[] = [
				'date' => $formattedDate, 'list' => ProjectHealthFood::with(['health', 'nutrtion' => function (Query $query) {
					$query->order(['id' => 'desc']);
				}])->where(['user_id' => $this->user_id])
					->whereIn('id', $foods_ids)
					->order(['id' => 'desc',])
					->select()
			];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 营养健康-详情
	 * @param Request $request
	 * @return void
	 */
	function healthDeail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectHealthFoodNutrition::with(['food'])->where(['food_id' => $id])->order(['id' => 'desc'])->find(); //最新的一条
		$this->writeJson($row);
	}
	
	/**
	 * 营养健康-详情-营养设置
	 * @param Request $request
	 * @return void
	 */
	function healthDetailNutrition(Request $request)
	{
		$food_id = $request->post('food_id/d');
		$weight = $request->post('weight');
		if (empty($weight)) {
			$this->writeJson(0, '重量不能为空');
		}
		Log::record('重量');
		Log::record($weight);
		$row = ProjectHealthFoodNutrition::where(['food_id' => $food_id])->findOrFail(); //最旧的一条
		$data = [
			'weight' => $weight,
			'food_id' => $row->food_id,
			'food' => $row->food,
			'calcium' => bcmul(bcdiv($row->calcium, '100', 10), (string)$weight, 2),
			'carbohydrates' => bcmul(bcdiv($row->carbohydrates, '100', 10), (string)$weight, 2),
			'cholesterol' => bcmul(bcdiv($row->cholesterol, '100', 10), (string)$weight, 2),
			'copper' => bcmul(bcdiv($row->copper, '100', 10), (string)$weight, 2),
			'dietary_fiber' => bcmul(bcdiv($row->dietary_fiber, '100', 10), (string)$weight, 2),
			'energy' => bcmul(bcdiv($row->energy, '100', 10), (string)$weight, 2),
			'fat' => bcmul(bcdiv($row->fat, '100', 10), (string)$weight, 2),
			'iron' => bcmul(bcdiv($row->iron, '100', 10), (string)$weight, 2),
			'magnesium' => bcmul(bcdiv($row->magnesium, '100', 10), (string)$weight, 2),
			'manganese' => bcmul(bcdiv($row->manganese, '100', 10), (string)$weight, 2),
			'phosphorus' => bcmul(bcdiv($row->phosphorus, '100', 10), (string)$weight, 2),
			'potassium' => bcmul(bcdiv($row->potassium, '100', 10), (string)$weight, 2),
			'protein' => bcmul(bcdiv($row->protein, '100', 10), (string)$weight, 2),
			'saturated' => bcmul(bcdiv($row->saturated, '100', 10), (string)$weight, 2),
			'sodium' => bcmul(bcdiv($row->sodium, '100', 10), (string)$weight, 2),
			'sugar' => bcmul(bcdiv($row->sugar, '100', 10), (string)$weight, 2),
			'trans_fat' => bcmul(bcdiv($row->trans_fat, '100', 10), (string)$weight, 2),
			'vitamin_c' => bcmul(bcdiv($row->vitamin_c, '100', 10), (string)$weight, 2),
			'zinc' => bcmul(bcdiv($row->zinc, '100', 10), (string)$weight, 2),
		];
		$this->writeJson($data);
	}
	
	/**
	 * 幸福感
	 * @param Request $request
	 * @return void
	 */
	function happy(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectHappyOut::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$arr[] = ['date' => $formattedDate, 'list' => ProjectHappyOut::whereDay('createtime', $row->time)->filter(function ($data) {
				$ids = $data->happy_ids;
				$count = count(array_filter(explode(',', $ids)));
				$data->text = "共计回答了{$count}个问题.";
			})->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select()];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 幸福感-详情
	 * @param Request $request
	 * @return void
	 */
	function happyDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectHappyOut::findOrFail($id);
		$ids = $row->happy_ids;
		$rows = ProjectHappy::withTrashed()->with(['parent'=>function (Query $query) {
			$query->removeOption('soft_delete');
		}])->whereIn('id', $ids)->select();
		$this->writeJson($rows);
	}
	
	/**
	 * 生活方式
	 * @param Request $request
	 * @return void
	 */
	function life(Request $request)
	{
		$date = $request->get('date', date("Y-m"));
		$rows = ProjectLifeOut::field('from_unixtime(createtime,"%Y-%m-%d") as time')->where(['user_id' => $this->user_id])->whereMonth('createtime', $date)->group('time')->order(['id' => 'desc'])->select();
		$arr = [];
		foreach ($rows as $row) {
			$dateString = $row->time;
			$date = DateTime::createFromFormat('Y-m-d', $dateString);
			$formattedDate = $date->format('Y年m月d日');
			$arr[] = ['date' => $formattedDate, 'list' => ProjectLifeOut::whereDay('createtime', $row->time)->filter(function ($data) {
				$ids = $data->life_ids;
				$count = count(array_filter(explode(',', $ids)));
				$data->text = "共计回答了{$count}个问题.";
			})->where(['user_id' => $this->user_id])->order(['id' => 'desc'])->select()];
		}
		$this->writeJson($arr);
	}
	
	/**
	 * 生活方式-详情
	 * @param Request $request
	 * @return void
	 */
	function lifeDetail(Request $request)
	{
		$id = $request->get('id/d');
		$row = ProjectLifeOut::findOrFail($id);
		$ids = $row->life_ids;
		$rows = ProjectLife::withTrashed()->with(['parent'=>function (Query $query) {
			$query->removeOption('soft_delete');
		}])->whereIn('id', $ids)->select();
		$this->writeJson($rows);
	}
}
